{$R-}

(*
	openSPort portNumber -- Open the serial port driver.
	By Harry Chesley.  DO NOT call the author!  Contact Apple Developer 
	Support on AppleLink "MacDTS" or on MCI "MacTech".

	ŠApple Computer, Inc. 1987
	All Rights Reserved.

	To compile and link this file using Macintosh Programmer's Workshop,

	pascal -w openPort.p
	link -m ENTRYPOINT -o HyperTerm -rt XCMD=0 -sn Main=openSPort openPort.p.o "{MPW}"Libraries:interface.o
	
*)

{$S openSPort } 	{ Segment name must be the same as the command name. }

unit DummyUnit;

interface

uses MemTypes, QuickDraw, OSIntf, HyperXCmd;

procedure EntryPoint(paramPtr: XCmdPtr);
	
implementation

type

Str31 = String[31];

procedure openSPort(paramPtr: XCmdPtr); forward;

procedure EntryPoint(paramPtr: XCmdPtr);

	begin
		openSPort(paramPtr);
	end;

procedure openSPort(paramPtr: XCmdPtr);

	var portNumber: integer;	
		inPort, outPort: integer;
		actualIn, actualOut: integer;
		i: integer;
		str: Str255;
		shakes: SerShk;

	{$I XCmdGlue.inc}

	procedure Fail(errMsg: Str255); { set theResult and quit }
		begin
			paramPtr^.returnValue := PasToZero(errMsg);
			exit(openSPort);
		end;

	begin
		if paramPtr^.paramCount <> 1 then Fail('parameter count is not 1');

		ZeroToPas(paramPtr^.params[1]^,str);		{ First parameter is port number. }
		portNumber := StrToNum(str);
		if (portNumber < 1) or (portNumber > 2) then Fail('invalid port number');

		if portNumber = 1 then
			begin
				inPort := -6; outPort := -7;
			end
		else
			begin
				inPort := -8; outPort := -9;
			end;
		if (OpenDriver('.AOut',actualOut) <> noErr) or (OpenDriver('.AIn',actualIn) <> noErr) then
			Fail('OpenDriver failed');
		if (actualOut <> outPort) or (actualIn <> inPort) then Fail('openDriver failed');
		with shakes do
			begin
				fXOn := 0; fCTS := 0; errs := 0; evts := 0; fInX := 0;
			end;
		if SerHShake(inPort,shakes) <> noErr then Fail('SerHShake failed');
		if SerHShake(outPort,shakes) <> noErr then Fail('SerHShake failed');
	end;

end.
